﻿(function () {

    function announcementCrudSrv(common) {
        var hostweburl = common.hostweburl;
        var appweburl = common.appweburl;
        var listName = common.listName;

        function _getDataFromList() {
            var deferred = $.Deferred();
            var scriptbase = hostweburl + "/_layouts/15/";
          

            function execCrossDomainRequest() {
                var restQueryUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('" + listName + "')/items?&$select=Title,Status&$orderby=Title asc&@target='" + hostweburl + "'";
                var executor = new SP.RequestExecutor(appweburl);
                executor.executeAsync({
                    url: restQueryUrl,
                    method: "GET",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    },
                    success: function (data, textStatus, xhr) {
                        var dData = JSON.parse(data.body).d.results;
                        deferred.resolve(dData);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        deferred.reject(xhr);
                    }
                });
            }

            $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
            
            return deferred;
        }

        return {
            getDataFromList: _getDataFromList
        }
    }

    angular.module('SpaDemo').factory('announcementCrudSrv', ['common', announcementCrudSrv]);;
  })();
